﻿using System.Collections;
using UnityEngine;
using UnityEngine.Networking;

/// <summary>
/// 水中花掉落物
/// </summary>
public class WaterFlowerResource : MonoBehaviour
{
    private Sprite sprite;
    private void Start()
    {
        //加载Mod内的资源
        StartCoroutine(LoadEntityResource());
    }
    public IEnumerator LoadEntityResource()
    {
        var uwr = UnityWebRequestAssetBundle.GetAssetBundle($"{Application.persistentDataPath}/Mod/{FirstMod.modName}/Res/modresource.ab");
        yield return uwr.SendWebRequest();

        // Get an asset from the bundle and instantiate it.
        AssetBundle bundle = DownloadHandlerAssetBundle.GetContent(uwr);
        var loadAsset = bundle.LoadAllAssets();
        foreach (var item in loadAsset)
        {
            if (item is Sprite sprite && sprite.name == "WaterFlowerResource")
            {
                this.sprite = sprite;
            }
        }
        gameObject.AddComponent<SpriteRenderer>().sprite = sprite;
        //创建配置类并添加到配置管理器，游戏内会及时更新相应数据
        var config = new EntityResourceConfig("水生花 花苞", sprite);
        ConfigurationManager.Instance.EntityResourceConfigs.Add(config.id, config);
        Destroy(gameObject);
    }
}